import 'package:flutter/material.dart';
import 'package:flutter_09_game/base/widget/base_get_builder.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/home/home_gameplay_tutorial/home_gameplay_tutorial_list/model_game_active.dart';
import 'package:flutter_09_game/pages/home/home_gameplay_tutorial/home_gameplay_tutorial_logic.dart';
import 'package:flutter_09_game/utils/extensions/num_ext.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'home_gameplay_tutorial_list_logic.dart';

class HomeGameplayTutorialListPage extends StatelessWidget {
  HomeGameplayTutorialListPage(
      {super.key, required this.type, required this.gameActive});

  final ModelGameActive? gameActive;
  late final HomeGameplayTutorialListLogic logic =
      Get.put(HomeGameplayTutorialListLogic(), tag: type);
  final String type;

  @override
  Widget build(BuildContext context) {
    return BaseGetBuilder<HomeGameplayTutorialListLogic>(
      init: logic,
      tag: type,
      backgroundColor: AppColor.pageBgColor,
      builder: (l) {
        // int id = type == LocaleKeys.lastDigitOddEven.tr
        //     ? 1
        //     : type == LocaleKeys.oneMinOddEven.tr
        //         ? 2
        //         : 3;
        // return Obx(() {
        //   ModelGameActive? gameActive = Get.find<HomeGameplayTutorialLogic>()
        //       .gameActiveList
        //       .firstWhereOrNull((item) => item.game?.id == id);
        //   // HomeGameplayTutorialLogic
        //   clog('message   ${gameActive?.toJson()}');
        return _buildTutorialOddItem(gameActive);
        // });
      },
    );
  }

//尾数单双
  Widget _buildTutorialOddItem(ModelGameActive? item) {
    return SingleChildScrollView(
      padding: EdgeInsets.all(16.w).copyWith(top: 0),
      child: Column(
        children: [
          _buildInfoItems(
              LocaleKeys.transferLimit.tr,
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _buildRichItem(
                    LocaleKeys.transferBet01.tr,
                  ),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(
                      LocaleKeys.transferBet01Detail2.tr.trArgs([
                        item?.limits?.transferBetLimitForTRXConfigKey?.min
                                ?.formatWallet() ??
                            '',
                        item?.limits?.transferBetLimitForTRXConfigKey?.max
                                ?.formatWallet() ??
                            ''
                      ]),
                      middleText: false,
                      showCir: true),
                  12.verticalSpaceFromWidth,
                  _buildRichItem(
                    LocaleKeys.transferBet02.tr,
                  ),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(
                      LocaleKeys.transferBet02Detail1.tr.trArgs([
                        item?.limits?.pageBetLimitForUSDTConfigKey?.min
                                ?.formatWallet() ??
                            '',
                        item?.limits?.pageBetLimitForUSDTConfigKey?.max
                                ?.formatWallet() ??
                            ''
                      ]),
                      middleText: false,
                      showCir: true),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(
                      LocaleKeys.transferBet02Detail2.tr.trArgs([
                        item?.limits?.pageBetLimitForTRXConfigKey?.min
                                ?.formatWallet() ??
                            '',
                        item?.limits?.pageBetLimitForTRXConfigKey?.max
                                ?.formatWallet() ??
                            ''
                      ]),
                      middleText: false,
                      showCir: true),
                  12.verticalSpaceFromWidth,
                  _buildRichItem(
                    LocaleKeys.transferInvalid.tr,
                  ),
                  // 12.verticalSpaceFromWidth,
                  // _buildRichItem(
                  //   LocaleKeys.transferIntegerOnly.tr,
                  // ),
                ],
              )),
          _buildInfoItems(
              LocaleKeys.bettingRule.tr,
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _buildRichItem(
                    LocaleKeys.bettingRuleDesc.tr,
                  ),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.bettingExample1.tr,
                      middleText: false, showCir: true),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.bettingExample2.tr,
                      middleText: false, showCir: true),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.bettingExample3.tr,
                      middleText: false, showCir: true),
                ],
              )),
          _buildInfoItems(
              LocaleKeys.lotteryRule.tr,
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Visibility(
                      visible: type == LocaleKeys.oneMinOddEven.tr,
                      child: _buildRichItem(
                        LocaleKeys.tronBlockRule20.tr,
                      ).marginOnly(bottom: 8.w)),
                  Visibility(
                      visible: type == LocaleKeys.oneMinOddEven.tr,
                      child: _buildRichItem(
                        LocaleKeys.tronBlockRule20Example.tr,
                      ).marginOnly(bottom: 8.w)),
                  Visibility(
                      visible: type == LocaleKeys.threeMinOddEven.tr,
                      child: _buildRichItem(
                        LocaleKeys.tronBlockRule60.tr,
                      ).marginOnly(bottom: 8.w)),
                  Visibility(
                      visible: type == LocaleKeys.threeMinOddEven.tr,
                      child: _buildRichItem(
                        LocaleKeys.tronBlockRule60Example.tr,
                      ).marginOnly(bottom: 8.w)),
                  _buildRichItem(
                    LocaleKeys.lotteryRuleDesc.tr +
                        LocaleKeys.lotteryExample1.tr,
                  ),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.lotteryExample2.tr,
                      middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(
                      type != LocaleKeys.lastDigitOddEven.tr
                          ? LocaleKeys.tronBlockRule20Even.tr
                          : LocaleKeys.lotteryExample3.tr,
                      middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.lotteryOddWin.tr,
                      middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.lotteryEvenWin.tr,
                      middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(
                      LocaleKeys.lotteryReturn.tr.trArgs(
                          [(gameActive?.game?.winRate ?? 1.0).toString()]),
                      middleText: false),
                ],
              )),
//中奖实例
          _buildInfoItems(
              LocaleKeys.lotteryExample.tr,
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _buildRichItem(LocaleKeys.exampleAmount1001.tr),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.exampleBetOdd.tr,
                      middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.exampleHash1.tr, middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.exampleResult1.tr,
                      middleText: false),
                  _buildRichItem(LocaleKeys.exampleWin.tr, middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(
                      LocaleKeys.exampleReturn1.tr.trArgs([
                        (gameActive?.game?.winRate ?? 1.0).toString(),
                        (1001 * (gameActive?.game?.winRate ?? 1.0)).toString()
                      ]),
                      middleText: false), //(gameActive?.game?.winRate ?? 1.0)
                ],
              )),
          8.verticalSpaceFromWidth,
          _buildInfoItems(
              null,
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _buildRichItem(LocaleKeys.exampleAmount2022.tr),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.exampleBetEven.tr,
                      middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.exampleHash2.tr, middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.exampleResult2.tr,
                      middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.exampleWin.tr, middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.exampleReturn2.tr.trArgs([ (gameActive?.game?.winRate ?? 1.0).toString(),
                    (2022 * (gameActive?.game?.winRate ?? 1.0)).toString()]),
                      middleText: false),
                ],

              )),

          8.verticalSpaceFromWidth,
          _buildInfoItems(
              null,
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _buildRichItem(LocaleKeys.exampleAmount1999.tr),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.exampleBetOdd.tr,
                      middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.exampleHash3.tr, middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.exampleResult3.tr,
                      middleText: false),
                  8.verticalSpaceFromWidth,
                  _buildRichItem(LocaleKeys.exampleLose.tr, middleText: false),
                ],
              )),
          8.verticalSpaceFromWidth,
          // _buildInfoItems(
          //     null,
          //     Column(
          //       crossAxisAlignment: CrossAxisAlignment.start,
          //       children: [
          //         _buildRichItem(LocaleKeys.exampleAmount1996.tr),
          //         8.verticalSpaceFromWidth,
          //         _buildRichItem(LocaleKeys.exampleBetEven.tr,
          //             middleText: false),
          //         8.verticalSpaceFromWidth,
          //         _buildRichItem(LocaleKeys.exampleHash4.tr, middleText: false),
          //         8.verticalSpaceFromWidth,
          //         _buildRichItem(LocaleKeys.exampleResult4.tr,
          //             middleText: false),
          //         8.verticalSpaceFromWidth,
          //         _buildRichItem(LocaleKeys.exampleLose.tr, middleText: false),
          //       ],
          //     )),
        ],
      ),
    );
  }

  Widget _buildInfoItems(String? title, Widget child) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Visibility(
            visible: title != null,
            child: SizedBox(
              height: 28.w,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  title ?? '',
                  style: TextStyle(
                    color: AppColor.textPrimary.withOpacity(0.94),
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ).marginOnly(top: 16.w, bottom: 4.w)),
        // 16.verticalSpaceFromWidth,
        // ,
        // 4.verticalSpaceFromWidth,
        Container(
          width: double.infinity,
          padding: EdgeInsets.all(12.w),
          decoration: BoxDecoration(
            color: Colors.black.withOpacity(0.3),
            borderRadius: BorderRadius.circular(12.w),
            border: Border(
              bottom: BorderSide(
                color: AppColor.textPrimary.withOpacity(0.15),
                width: 1.w,
              ),
            ),
          ),
          child: child,
        )
      ],
    );
  }

  /// 构建提示项
  // Widget _buildTipItem(String text,
  //     {String? highlightText, List<String>? highlightTexts}) {
  //   final highlights =
  //       highlightTexts ?? (highlightText != null ? [highlightText] : []);
  //
  //   return Padding(
  //     padding: EdgeInsets.only(bottom: 4.w),
  //     child: RichText(
  //       text: TextSpan(
  //         style: TextStyle(
  //           fontFamily: 'PingFang SC',
  //           fontSize: 12.sp,
  //           fontWeight: FontWeight.w500,
  //           color: AppColor.textPrimary.withOpacity(0.7),
  //           height: 1.55,
  //         ),
  //         children: _buildHighlightedText(text, highlights),
  //       ),
  //     ),
  //   );
  // }

  Widget _buildRichItem(
    String text, {
    bool middleText = true,
    bool showCir = false,
  }) {
    final List<InlineSpan> spans = [];
    final hStyle = TextStyle(
      fontSize: 12.sp,
      fontWeight: FontWeight.w600,
      color: AppColor.primary,
      // height: 1.55,
    );
    final style = TextStyle(
        fontSize: 12.sp,
        fontWeight: middleText ? FontWeight.w500 : FontWeight.w400,
        color: AppColor.textPrimary.withOpacity(middleText ? 0.7 : 0.3));
    // 匹配 {{xxx}} 这种标记
    final regex = RegExp(r'{(.*?){');
    int lastMatchEnd = 0;

    if (showCir) {
      spans.add(TextSpan(
        text: '• ',
        style: style,
      ));
    }

    for (final match in regex.allMatches(text)) {
      // 添加普通文本
      if (match.start > lastMatchEnd) {
        spans.add(TextSpan(
          text: text.substring(lastMatchEnd, match.start),
          style: style,
        ));
      }
      // 添加高亮文本
      spans.add(TextSpan(
        text: match.group(1), // 拿到 {{xxx}} 中的 xxx
        style: hStyle,
      ));
      lastMatchEnd = match.end;
    }

    // 添加最后剩余的普通文本
    if (lastMatchEnd < text.length) {
      spans.add(TextSpan(
        text: text.substring(lastMatchEnd),
        style: style,
      ));
    }

    return RichText(text: TextSpan(style: style, children: spans));
  }

// /// 构建高亮文本
// List<TextSpan> _buildHighlightedText(String text, List<String> highlights) {
//   List<TextSpan> spans = [];
//   String remainingText = text;
//
//   for (String highlight in highlights) {
//     final index = remainingText.indexOf(highlight);
//     if (index != -1) {
//       // 添加高亮前的文本
//       if (index > 0) {
//         spans.add(TextSpan(text: remainingText.substring(0, index)));
//       }
//       // 添加高亮文本
//       spans.add(TextSpan(
//         text: highlight,
//         style: TextStyle(
//           fontFamily: 'PingFang SC',
//           fontSize: 12.sp,
//           fontWeight: FontWeight.w600,
//           color: AppColor.primary,
//         ),
//       ));
//       // 更新剩余文本
//       remainingText = remainingText.substring(index + highlight.length);
//     }
//   }
//
//   // 添加剩余文本
//   if (remainingText.isNotEmpty) {
//     spans.add(TextSpan(text: remainingText));
//   }
//
//   return spans;
// }
}
